package streamforce.workload.test;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import streamforce.crypto.event.StockEvent;

public class StreamManager {
	static final int aggType=2; 
	int streamId; 
	int currentCount; 
	int currentIdx; 
	String baseFile; 
	ArrayList<StockEvent> se; 
	//currentIndex = 0..50
	public StreamManager(int streamId, int currentIdx) throws Exception{
		this.streamId = streamId;
		this.currentCount = 0;
		this.currentIdx = currentIdx;
		if (aggType==2)
			baseFile = "/DATA/NewEncFiles/enc_file_"+streamId;
		else 
			baseFile = "/DATA/NewEncFiles/enc_file_agg"+aggType+"_"+streamId;
		se = new ArrayList<StockEvent>();
		this.initialization(); 
	}
	
	private void initialization() throws Exception{
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				baseFile + "_" + this.currentIdx));
		this.se = (ArrayList<StockEvent>)ois.readObject(); 
	}
	
	public StockEvent getNextEvent() throws Exception{
		if (this.currentCount>0 && this.currentCount%200==0){
			this.currentIdx++; 
			this.initialization(); 
		}		
		StockEvent returnedEvent = this.se.get(this.currentCount%200);
		this.currentCount++;
		System.out.format("(%d,%d)\n", returnedEvent.getTimestamp(), returnedEvent.getStockId()); 
		return returnedEvent; 
	}
}
